package com.example.mr5.frogge;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.util.Arrays;

/**
 * Created by Mr.5
 * Date：2016/5/9.
 * Time：09:55
 */
public class GameView extends SurfaceView implements View.OnTouchListener {

    private int[] frogLocal = new int[]{1, 1, 1, 0, -1, -1, -1};//初始状态
    private int[] endFrogLocal = new int[]{-1,-1,-1,0,1,1,1};//结束状态
    private Frog[] frogs = new Frog[7];
    private static final int ROW = 7;
    private int Frog_WIDTH;        //青蛙宽度

    private int height;
    private int width;

    MediaPlayer mediaPlayer ;//叫声



    public GameView(Context context) {
        super(context);
        getHolder().addCallback(callback);
        initGameView();

    }
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(callback);
        initGameView();
    }

    private void initGameView() {
        mediaPlayer = new MediaPlayer();
        setZOrderOnTop(true);//设置背景透明
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setOnTouchListener(this);
    }

    private void reDraw() {
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//清除画布
        Paint paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);    //设置抗锯齿
        for (int i = 0; i < ROW; i++) {
            Frog one = new Frog(frogLocal[i], i);
            frogs[i] = one;

            //绘制背景图片
            RectF rectF = new RectF(one.getX() * Frog_WIDTH, (one.getX()+0.5f-i) * Frog_WIDTH,
                    (one.getX() + 1) * Frog_WIDTH, (one.getX() + 1.2f - i) * Frog_WIDTH);
            Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.stone);
            canvas.drawBitmap(bitmap, null, rectF, null);

            switch (one.getType()) {
                case Frog.LEFT_SIZE:
                    paint.setColor(0xFFFF0000);
                    //绘制左侧青蛙
                    RectF rectF1 = new RectF(one.getX() * Frog_WIDTH, (one.getX() - i) * Frog_WIDTH,
                            (one.getX() + 1) * Frog_WIDTH, (one.getX() + 1 - i) * Frog_WIDTH);
                    Bitmap bitmap1 = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.frog_left);
                    canvas.drawBitmap(bitmap1, null, rectF1, null);
                    break;
                case Frog.RIGHT_SIZE:
                    paint.setColor(0xFFFF0000);
                    //绘制右侧青蛙
                    RectF rectF2 = new RectF(one.getX() * Frog_WIDTH, (one.getX() - i) * Frog_WIDTH,
                            (one.getX() + 1) * Frog_WIDTH, (one.getX() + 1 - i) * Frog_WIDTH);
                    Bitmap bitmap2 = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.frog_right);
                    canvas.drawBitmap(bitmap2, null, rectF2, null);
                    break;
                case 0:
                    break;

                default:
                    break;
            }
        }
        getHolder().unlockCanvasAndPost(canvas);    //解除锁定，绘制界面
    }

    /**
     * SurfaceHolder.Callback
     * 绘制内容
     */
    SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {

        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            reDraw();
        }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
//            DOT_WIDTH = arg2/(COL+1);
//            reDraw();
        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x, y;
            y = (int) (event.getY() / Frog_WIDTH);
            x = (int) (event.getX() / Frog_WIDTH);
            if (x + 1 <= ROW && y + 1 <= 1) {    //点击青蛙
                move(x);
                reDraw();
                checkEnd();
            }
        }
        return false;
    }

    /*
    *移动判断
    * */
    private void move(int x){
        if(frogs[x].getType() == Frog.LEFT_SIZE){
            switch (x){
                case 5:
                    if(frogLocal[6]==0){
                        frogLocal[6]=1;
                        frogLocal[5]=0;
                    }
                    break;
                case 6:
                    break;
                default:
                    if(frogLocal[x+1]==0){
                        frogLocal[x]=0;
                        frogLocal[x+1]=1;
                    }else if(frogLocal[x+2]==0){
                        frogLocal[x]=0;
                        frogLocal[x+2]=1;
                    }
                    break;
            }
        }else if(frogs[x].getType() == Frog.RIGHT_SIZE){
            switch (x){
                case 1:
                    if(frogLocal[0]==0){
                        frogLocal[0]=-1;
                        frogLocal[1]=0;
                    }
                    break;
                case 0:
                    break;
                default:
                    if(frogLocal[x-1]==0){
                        frogLocal[x]=0;
                        frogLocal[x-1]=-1;
                    }else if(frogLocal[x-2]==0){
                        frogLocal[x]=0;
                        frogLocal[x-2]=-1;
                    }
                    break;
            }
        }
    }

    private void checkEnd(){
        if(Arrays.equals(frogLocal,endFrogLocal)){
            Toast.makeText(getContext(),"恭喜您成功了！您的智力媲美爱因斯坦！",Toast.LENGTH_SHORT).show();
        }
    }

    public void ReStartGame(){
        frogLocal = new int[]{1, 1, 1, 0, -1, -1, -1};
        reDraw();
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
    {
        height = MeasureSpec.getSize(heightMeasureSpec);//获取屏幕宽高
        width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width,height/5);
        Frog_WIDTH = (Math.min(width, height) - 7) / Config.LINES;

    }
}
